package cn.tedu.ymg.product.service.impl;

import cn.tedu.ymg.commons.ex.ServiceException;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.util.PageInfoToPageDataConverter;
import cn.tedu.ymg.commons.web.ServiceCode;
import cn.tedu.ymg.product.mapper.MealMapper;
import cn.tedu.ymg.product.pojo.entity.Meal;
import cn.tedu.ymg.product.pojo.param.MealAddNewParam;
import cn.tedu.ymg.product.pojo.param.MealUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.MealListItemVO;
import cn.tedu.ymg.product.pojo.vo.MealStandardVO;
import cn.tedu.ymg.product.service.IMealService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class MealServiceImpl implements IMealService {

    @Autowired
    MealMapper mealMapper;



    @Override
    public void addNew(MealAddNewParam mealAddNewParam) {

        log.debug("开始处理【添加套餐的业务】，参数", mealAddNewParam);

        //将套餐写入到数据库中
        Meal meal = new Meal();
        BeanUtils.copyProperties(mealAddNewParam, meal);

        int rows = mealMapper.insert(meal);
        if (rows !=1){
            String Meal1 = "添加套餐失败，服务器忙，请稍后再试！";
            log.warn(Meal1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Meal1);
        }
        log.debug("将新的套餐数据写入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除套餐】的业务，参数：{}", id);
        Meal meal = mealMapper.selectById(id);
        if (meal ==null){
            String Meal1 = "删除套餐失败，Id不存在";
            log.warn(Meal1);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Meal1);
        }

        int rows = mealMapper.deleteById(id);
        if (rows!=1){
            String Meal1 = "删除套餐失败，服务器忙，请稍后再试！";
            log.warn(Meal1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Meal1);
        }
        log.debug("从数据库删除套餐，完成！");
    }

    @Override
    public void updateInfoById(Long id, MealUpdateInfoParam mealUpdateInfoParam) {

        log.debug("开始处理【修改套餐】的业务，ID:{},新数据：{}", id, mealUpdateInfoParam);

        //检查套餐是否存在，如果不存在，则抛出异常
        QueryWrapper<Meal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = mealMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的套餐数量，结果：{}", countById);
        if (countById == 0) {
            String Meal = "修改套餐失败，套餐数据不存在！";
            log.warn(Meal);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Meal);
        }

        //执行修改
        Meal meal = new Meal();
        BeanUtils.copyProperties(mealUpdateInfoParam, meal);
        meal.setId(id);
        int rows = mealMapper.updateById(meal);
        if (rows!=1){
            String Meal1 = "修改套餐失败，服务器忙，请稍后再试！";
            log.warn(Meal1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Meal1);
        }
        log.debug("从数据库修改套餐，完成！");
    }

    @Override
    public MealStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询套餐】的业务，参数：{}", id);
        MealStandardVO queryResult = mealMapper.getStandardById(id);
        if (queryResult==null){
            String Meal = "查询套餐失败，套餐数据不存在！";
            log.warn(Meal);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,Meal);
        }
        return queryResult;
    }

    @Override
    public PageData<MealListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<MealListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询套餐列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<MealListItemVO> list =  mealMapper.list();
        PageInfo<MealListItemVO> pageInfo = new PageInfo<>(list);
        PageData<MealListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
